package com.yoomiito.app.widget.dialog;

import android.Manifest;
import android.content.Context;
import android.os.Build;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.yoomiito.app.R;
import com.yoomiito.app.base.BaseActivity;
import com.yoomiito.app.utils.AllUtils;
import com.yoomiito.app.utils.ImageLoader;
import com.yoomiito.app.utils.wx.ShareUtils;

import java.util.List;

/**
 *
 */
public class ShareBottomDialog_2 extends BaseDialog implements View.OnClickListener {

    private OnShareItemClickListener mOnShareItemClickListener;
    private View mPivView;
    private List<String> mPicUrls;
    private String mVideoUrl;
    private String mShareContent;
    private boolean showAlbum = true;
    private ShareDialog shareDialog;

    public ShareBottomDialog_2(@NonNull Context context) {
        this(context, 0);
    }

    public ShareBottomDialog_2(@NonNull Context context, int themeResId) {
        super(context, R.style.dialog_2);
    }

    protected ShareBottomDialog_2(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    public View setView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_share, null);
        view.findViewById(R.id.item_share_wx).setOnClickListener(this);
        view.findViewById(R.id.item_share_dynamic).setOnClickListener(this);
        mPivView = view.findViewById(R.id.item_share_photo);
        mPivView.setVisibility(showAlbum ? View.VISIBLE : View.GONE);
        mPivView.setOnClickListener(this);
        view.findViewById(R.id.cancel).setOnClickListener(this);
        setCanceledOnTouchOutside(true);
        setmGravity(Gravity.BOTTOM);
        setWidthPercent(1);
        return view;
    }

    @Override
    public void onClick(View v) {
        int i = 0;
        switch (v.getId()) {
            case R.id.item_share_wx:
                i = 1;
                if (Build.VERSION.SDK_INT >= 23) {
                    requestPermission(i);
                } else {
                    ShareUtils.shareWX((BaseActivity)mContext,mPicUrls,mShareContent);
                }
                break;
            case R.id.item_share_dynamic:
                i = 2;
                AllUtils.copyText(mContext, mShareContent);
                if (Build.VERSION.SDK_INT >= 23) {
                    requestPermission(i);
                } else {
                   shareDynamic();
                }
                break;
            case R.id.item_share_photo:
                i = 3;
                if (Build.VERSION.SDK_INT >= 23) {
                    requestPermission(i);
                } else {
                    loadPic();
                }
                break;
            case R.id.cancel:
                dismiss();
                return;
        }
        dismiss();
        if (mOnShareItemClickListener != null) {
            mOnShareItemClickListener.onClick(i);
        }
    }

    private void requestPermission(int type) {
        AllUtils.requestPermission((BaseActivity)mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE)
                .subscribe(aBoolean -> {
                    if (type == 1) {
                        ShareUtils.shareWX((BaseActivity)mContext,mPicUrls,mShareContent);
                    } else {
                        shareDynamic();
                    }
                });
    }

    private void shareDynamic(){
        loadPic();
        if (shareDialog == null) {
            shareDialog = new ShareDialog(mContext);
        }
        shareDialog.show();
    }

    private void loadPic() {
        if (mPicUrls != null && mPicUrls.size() > 0) {
            ImageLoader.getInstance().downloadPic(mContext, mPicUrls);
            Toast.makeText(mContext, "保存成功", Toast.LENGTH_SHORT).show();
        }
    }

    public void setOnShareItemClickListener(OnShareItemClickListener onShareItemClickListener) {
        mOnShareItemClickListener = onShareItemClickListener;
    }

    public interface OnShareItemClickListener {
        void onClick(int type);
    }

    public void setPicUrls(List<String> picUrls) {
        mPicUrls = picUrls;
    }

    public void setVideoUrl(String videoUrl) {
        mVideoUrl = videoUrl;
    }

    public void setShareContent(String shareContent) {
        mShareContent = shareContent;
    }

    public void setShowAlbum(boolean showAlbum) {
        this.showAlbum = showAlbum;
        if (mPivView != null) mPivView.setVisibility(showAlbum ? View.VISIBLE : View.GONE);
    }
}
